/**
 * 
 */
package grid;

import alg.Evaluation;
import java.text.DateFormat;
import java.util.*;
import org.jgap.Configuration;
import org.jgap.Genotype;
import org.jgap.IChromosome;
import org.jgap.Population;
import util.GridConf;
import util.Utils;
import static grid.Constants.*;

/**
 * @author Darko
 * 
 */
public class Collector {

    public static final String OUTPUT_FILENAME = "final_population" + EXTENSION;
    private static List<String> inputPopulationFiles = new LinkedList<String>();

    private static void setup(String[] args) {
        if (args.length < 2) {
            throw new IllegalArgumentException();
        }
        int islands = Integer.parseInt(args[0]);
        if (islands <= 0) {
            throw new IllegalArgumentException();
        }
        int epochs = Integer.parseInt(args[1]);
        if (epochs <= 0) {
            throw new IllegalArgumentException();
        }
        for (int i = 1; i <= islands; i++) {
            inputPopulationFiles.add("population" + i + "_" + epochs + OUTPUT_POSTFIX);
        }
    }

    public static void main(String[] args) throws Exception {
        setup(args);
        Configuration conf = Utils.getDefaultConfiguration(
                20, new Evaluation(GridConf.getDwConfiguration()));
        List<IChromosome> chromosomes = new ArrayList<IChromosome>();
        for (int i = 0; i < inputPopulationFiles.size(); i++) {
            chromosomes.add(Utils.loadGenotypeFromGZIP(inputPopulationFiles.get(i), conf).getFittestChromosome());
        }
        Genotype finalPop = new Genotype(conf,
                new Population(conf, chromosomes.toArray(new IChromosome[0])));
        String outputPopulationFile = OUTPUT_FILENAME;
        Utils.saveGenotypeToGZIP(outputPopulationFile, finalPop);
    }
}
